package yunplus.io.hchome.model.storage;

import android.content.Context;
import android.support.annotation.NonNull;
import android.text.TextUtils;

/**
 * Created by yfsoft on 2016/12/16.
 */

public class DomainShared {

    private static final String TAG = "DomainShared";

    private static final String KEY_DOMAIN = "SERVER_DOMAIN";

    private static final String KEY_GATEWAY_TYPE = "GATEWAY_TYPE";

    private DomainShared(){}

    private static String _domain;

    private static String _gatewayType;

    public static void saveDomain(@NonNull Context ctx, @NonNull String domain){
        SharedWrapper sharedWrapper = SharedWrapper.with(ctx, TAG);
        sharedWrapper.setString(KEY_DOMAIN, domain);
        _domain = domain;
    }

    public static boolean isSavedDomain(@NonNull Context ctx){
        if (!TextUtils.isEmpty(_domain)) {
            return true;
        }
        SharedWrapper sharedWrapper = SharedWrapper.with(ctx, TAG);
        _domain = sharedWrapper.getString(KEY_DOMAIN, null);
        _gatewayType = sharedWrapper.getString(KEY_GATEWAY_TYPE, "wlan");
        if (!TextUtils.isEmpty(_domain)) {
            return true;
        }
        return false;
    }

    public static String getDomain(@NonNull Context context) {
        if (!isSavedDomain(context)) {
            _domain = SharedWrapper.with(context, TAG).getString(KEY_DOMAIN, null);
        }
        return _domain;
    }

    public static String getGatewayType(@NonNull Context context) {
        if (!isSavedDomain(context)) {
            _gatewayType = SharedWrapper.with(context, TAG).getString(KEY_GATEWAY_TYPE, "wlan");
        }
        return _gatewayType;
    }

    public static String setGatewayType(@NonNull Context ctx, @NonNull String gatewayType) {
        SharedWrapper sharedWrapper = SharedWrapper.with(ctx, TAG);
        sharedWrapper.setString(KEY_GATEWAY_TYPE, gatewayType);
        _gatewayType = gatewayType;
        return _gatewayType;
    }

    public static void removeDomain(@NonNull Context ctx) {
        SharedWrapper.with(ctx, TAG).clear();
    }

}
